package com.cloud.basic.redis;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class RedisUtil {

    @Autowired
    private JedisPool jedisPool;

    public String get(String key){
        Jedis jedis = null;
        String value = null;
        try {
            jedis = jedisPool.getResource();
            value = jedis.get(key);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
        return value;
    }

    /**
     * 获取存储在哈希表中指定字段的值
     *
     * @param key
     * @param field
     * @return
     */
    public String hGet(String key, String field) {
        Jedis jedis = null;
        String value = null;
        try {
            jedis = jedisPool.getResource();
            value = jedis.hget(key, field);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
        return value;
    }

    /**
     * 获取所有给定字段的值
     *
     * @param key
     * @return
     */
    public Map<String, String> hGetAll(String key) {
        Jedis jedis = null;
        Map<String,String> value = null;
        try {
            jedis = jedisPool.getResource();
            value = jedis.hgetAll(key);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
        return value;
    }


    public void hSet(String key, String hashKey, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.hset(key,hashKey,value);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }

    }

    public void hSetMaps(String key, Map<String, String> maps) {
        Jedis jedis = null;
        Map<String,String> value = null;
        try {
            jedis = jedisPool.getResource();
            jedis.hmset(key,maps);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
    }

    /**
     * 删除一个或多个哈希表字段
     *
     * @param key
     * @param fields
     * @return
     */
    public Long hDelete(String key, String... fields) {
        Jedis jedis = null;
        Long value = null;
        try {
            jedis = jedisPool.getResource();
            value = jedis.hdel(key, fields);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
        return value;
    }

    /**
     * 查看哈希表 key 中，指定的字段是否存在
     *
     * @param key
     * @param field
     * @return
     */
    public boolean hExists(String key, String field) {
        Jedis jedis = null;
        boolean value = true;
        try {
            jedis = jedisPool.getResource();
            value = jedis.hexists(key,field);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
        return value;
    }

    /**
     * 获取所有哈希表中的字段
     *
     * @param key
     * @return
     */
    public Set<String> hKeys(String key) {
        Jedis jedis = null;
        Set<String> value = null;
        try {
            jedis = jedisPool.getResource();
            value = jedis.keys(key);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
        return value;
    }

    /**
     * 获取哈希表中字段的数量
     *
     * @param key
     * @return
     */
    public Long hSize(String key) {
        Jedis jedis = null;
        Long value = null;
        try {
            jedis = jedisPool.getResource();
            value = jedis.hlen(key);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
        return value;
    }

    /**
     * 获取哈希表中所有值
     *
     * @param key
     * @return
     */
    public List<String> hValues(String key) {
        Jedis jedis = null;
        List<String> value = null;
        try {
            jedis = jedisPool.getResource();
            value = jedis.hvals(key);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
        return value;
    }

    public void set(String key,String value){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key,value);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
    }

    public void set(String key,String value,int second){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key,value);
            jedis.expire(key,second);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
    }

    public Long incr(String key){
        Jedis jedis = null;
        Long incr = null;
        try {
            jedis = jedisPool.getResource();
            incr = jedis.incr(key);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
        return incr;
    }

    /**
     * 获取redis的key
     * @param table  表名
     * @param id     表的唯一ID
     * @param filed  字段名
     * @return
     */
    public String getKeyName(String table,Long id,String filed){
        return new StringBuilder(table).append(":").append(id).append(":").append(filed).toString();
    }


}
